const {
  find,
  update,
  remove,
  add
} = require('../../faas');

exports.cloud_register = async (dir, file, params) => {
  const findResponse = await find(dir, file, params);
  if (findResponse.flag) {
    return {
      flag: false,
      msg: '已注册'
    }
  } else {
    const r = await add(dir, file, params);
    console.log('cloud_register', r)
    if (r) {
      return {
        flag: true,
        msg: '成功'
      }
    } else {
      return {
        flag: false,
        msg: '失败'
      }
    }
  }
}

exports.cloud_login = async (dir, file, params) => {
  const r = await find(dir, file, params);
  console.log('cloud_read', r)
  if (r.flag == true) {
    if (JSON.parse(r.data)._collections[0].upwd === params.upwd) {
      return {
        flag: true,
        msg: '登录成功'
      }
    } else {
      return {
        flag: false,
        msg: '密码不正确'
      }
    }
  } else {
    return {
      flag: false,
      msg: '失败'
    }
  }
}

exports.cloud_change = async (dir, file, params) => {
  const r = await update(dir, file, params);
  console.log('cloud_change', r)
  if (r) {
    return {
      flag: true,
      msg: '修改密码成功'
    }
  } else {
    return {
      flag: false,
      msg: '失败'
    }
  }
}